<?php
/*
 * 账户提现
 */
class App_Account_Payment extends Data_Item 
{
    private $_type, $_nid;
    private $_uid;

    public function __construct ($uid = NULL)
    {
        parent::__construct();
        $this->_commTableName = 'user_v2_account_payment';
        $this->_uid  = (int) $uid;
        $this->_type = 'CASH';
    }

    public function setGroupType ($type, $nid = NULL)
    {
        $this->_type = $type;
        $this->_nid  = $nid;
        return $this;
    }

    public function info ($nid)
    {
        $info = $this->item(array('id', '=', $nid));
        if (empty($info))
            return array();

        $info['statusName'] = self::statusName($info['status']);
        $info['user']       = User::getUserFromUid($info['uid'])->getUserInfo();

        return $info;
    }

    public function countRequests($status = -1)
    {
        $where = array(
            array('uid', '=', $this->_uid),
            array('type', '=', $this->_type)
        );
            
        if ($status >= 0)
            $where[] = array('status', '=', $status);

        return $this->counts($where);
    }

    public function listRequests($status = -1, $offset = 0, $limit = 10)
    {
        $where = array(
            array('uid', '=', $this->_uid),
            array('type', '=', $this->_type)
        );
            
        if ($status >= 0)
            $where[] = array('status', '=', $status);

        $records = $this->items($where, array('created', 'desc'), $offset, $limit);

        $users = array();
        foreach ($records as & $rs)
        {
            $uid = $rs['uid'];
            if (! array_key_exists($uid, $users))
            {
                $user = User::getUserFromUid($uid);
                $users[$uid] = $user->getUserInfo();
            }

            $rs['user'] = Arr::get($users, $uid);
            $rs['statusName'] = self::statusName($rs['status']);
        }

        return $records;
    }

    public function addRequest ($accountName, $userName, $bankName, $moneyYuan)
    {
        $moneyFen = (int) ($moneyYuan * 100);
        if ($moneyFen <= 0)
            return FALSE;

        $row      = array(
            'uid' => $this->_uid,
            'accountName' => $accountName,
            'userName'    => $userName,
            'bankName'    => $bankName,
            'money'       => $moneyFen,
            'type'        => $this->_type,
            'status'      => 0,
            'comments'    => '',
            'created'     => DB::expr('unix_timestamp(now())'),
            'changed'     => DB::expr('unix_timestamp(now())'),
        );

        App_Account::addAccountCoin($this->_uid, 0 - $moneyFen, "申请提现,从账户扣除余额");

        return $this->insert($row);
    }

    public function handleRequest ($nid, $statusId, $comment = '')
    {
        $item = $this->info($nid);            
        if (empty($item))
            return FALSE;

        $status = $item['status'];
        if ($status == 0 && $statusId == 10)
        {
            $data = array('status' => $statusId, 'comments' => $comment);
            return $this->update($data, array('id', '=', $nid)); 
        }
        else if ($status == 10 && ($statusId == 20 || $statusId == 30))
        {
            $rows = $this->update(array('status' => $statusId, 'comments' => $comment), array('id', '=', $nid)); 

            // 申请失败，退款到用户账户
            if ($statusId == 30)
            {
                $moneyFen = $item['money'];
                $userUid  = $item['uid'];
                App_Account::addAccountCoin($userUid, $moneyFen, "提现失败, 返还账户余额");
            }

            return $rows;
        }
        else 
        {
            return false;
        }
    }

    public static function bankList ()
    {
        $query = DB::select()
            ->from('config_system')
            ->where('type', '=', 'bank')
            ->order_by('created', 'asc')
            ->execute();

        return $query->as_array();
    }

    public static function statusName ($statusId)
    {
        switch($statusId)
        {
            case 0:
                return '申请中';
                break;

            case 10:
                return '处理中';
                break;

            case 20:
                return '提现成功';
                break;

            case 30:
            default:
                return '提现失败';
                break;
        }
    }
}
